//
//  ZMRouteUtils.swift
//  Reader17kSDK
//
//  Created by Ink on 2020/11/9.
//

import UIKit

let RouteBookDetail = "kpath://book/(\\d+)"
let RouteReaderTarget = "kpath://reader/(\\d+)/chapterId/(\\d+)"

let kTabIndex = "tabIndex"

protocol RouteTargetProtocol {
    
    var target: String { get }
    var params: [String : Any]? { get }
}

extension RouteTargetProtocol {
    var target: String { "" }
    var params: [String : Any]? { nil }
    func openRoute(conduct: ((_ url:String) -> String)? = nil) {
        target.openRoute(params: params, conduct: conduct)
    }
}

struct RouteReader: RouteTargetProtocol {
    let bookId: Int
    var chapterId: Int?
    var target: String {
        var temptarget = RouteReaderTarget.replacingOccurrences(of: "reader/(\\d+)", with: "reader/\(bookId)")
        if let chapterId = chapterId  {
            temptarget = temptarget.replacingOccurrences(of: "chapterId/(\\d+)", with: "chapterId/\(chapterId)")
        }
        return temptarget
    }
}

extension ZMRoute {
    
    class func open(bookId: Int, chapterId: Int, bookName: String, isVoice: Bool? = false) {
        RouteReader(bookId: bookId, chapterId: chapterId).openRoute {
            $0.merge(params: ["bookName" : bookName, "isVoice" : isVoice ?? false])
        }
    }
}

extension String {
    
    func merge(tabSelectedIndex: Int) -> Self {
        merge(params: [kTabIndex : "\(tabSelectedIndex)"])
    }
}

extension ZMRoute {
    
    /// 初始化处理
    class func initUrlRoute() {
        
        initUrlRouteRegister()
        initUrlRouteHandler()
    }
    
    // 注册
    private class func initUrlRouteRegister() {
        register {
            BookReaderVC.self
            BookDetailVC.self
        }
    }
    
    // 解析回调处理
    private class func initUrlRouteHandler() {
        shared.urlRouteHandleOver = { (vc ,url, modal, params) in
            
            print("Handler url: \(url), params: \(String(describing: params)), vc: \(String(describing: vc?.className))")
            
//            let tab = T.shared.getTab()
//
//            if let tabIndex = (params?[kTabIndex] as? NSString)?.integerValue {
//                tab.update(selected: tabIndex)
//            }
//
//            if let vc = vc {
//                if modal == true {
//                    if let _ = tab.navigationController?.presentingViewController {
//                        ZMRoute.present(vc, fromViewController: tab.navigationController?.presentingViewController, animated: true, completion: nil)
//                    }
//                }
//                else {
//                    guard let nav = tab.navigationController else{
//                        return
//                    }
//                    nav.setViewControllers(nav.children + [vc], animated: true)
//                }
//            }
//            else {
//                print("解析失败:\(url)")
//            }
//
//            if let nav = tab.navigationController,tab.navigationController?.presentingViewController == nil {
//                ZMRoute.present(nav, fromViewController: ReaderColSDK.shared.presentingViewController, animated: true, completion: nil)
//            }
        }
    }
}
